<?php

include_once('../@base/portal.php');
include_once('../login/login.php');

$user = $post->user;
$data = $post->data;
$login = login($user->number, $user->passwd);
if ($login['status'] == 0 && $login['userbase']['level'] > 0) {
    output(insertData($data));
} else {
    output(['status' => 1]);
}

function insertData($data)
{
    // SQL Head
    $sql = "INSERT INTO oprec_inspect_record(number, clientName, clientGroup, clientOffice, category, value, remark, comment, seat, sector, begin, duration, time, userName, userGroup, userOffice, level, type, status) VALUES ";
    // INSERT VALUES
    foreach ($data->categories as $category) {
        $remark = json_encode($category->remark, JSON_UNESCAPED_UNICODE);
        $sql .= "(null,'{$data->clientName}','{$data->clientGroup}','{$data->clientOffice}','{$category->title}',{$category->value},'{$remark}','{$category->comment}','{$data->seat}','{$data->sector}','{$data->begin}',{$data->duration},'{$data->time}','{$data->userName}','{$data->userGroup}','{$data->userOffice}','{$data->level}','{$data->type}',0),";
    }
    /// Trim last ',' and append ';'
    $sql = rtrim($sql, ',');
    $sql .= ';';
    // If save SQL to file
    if (conf('debug')) logfile('LOGIN_SQL', $sql);
    // Do query
    $res = exec_sql($sql);
    return array('status' => 0, 'content' => $res);
}

/*
INSERT INTO oprec_inspect_record(number, clientName, clientGroup, clientOffice, category, item, subitem, value, remark, comment, seat, sector, begin, duration, time, userName, userGroup, userOffice, type, status) VALUES ([value-1],[value-2],[value-3],[value-4],[value-5],[value-6],[value-7],[value-8],[value-9],[value-10],[value-11],[value-12],[value-13],[value-14],[value-15],[value-16],[value-17],[value-18],[value-19],[value-20])
*/

/*
{
    clientName: c.name,
    clientGroup: c.clientGroup,
    clientOffice: c.clientOffice,
    clientRank: c.clientRank,
    categories: categories,
    seat: this.formData.position[0],
    sector: this.formData.position[1] ? this.formData.position[1] : "",
    begin: this.formData.start.getTime(),
    duration: 0,
    time: this.formData.start.getTime(),
    userName: u.name,
    userGroup: u.clientGroup,
    userOffice: u.clientOffice,
    type: "现场",
    status: 0,
}
*/